package com.zy.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.zy.pojo.User;
import com.zy.service.user.UserService;
import com.zy.tools.Constants;
import com.zy.tools.PageSupport;


@Controller
public class UserController {
	private Logger logger = Logger.getLogger("UserController.class");
	@Resource
	private UserService userService;
	
	@RequestMapping(value="/denglu",method=RequestMethod.GET)
	public String dly(){
		logger.info("登录页");
		return "denglu";
	}
	@RequestMapping(value="/deng",method=RequestMethod.GET)
	public String denglu(String name,String mima,Model model,HttpSession session,HttpServletRequest request){
		User user = userService.getUserName(name,mima);
		if(user!=null){
			session.setAttribute(Constants.USER_SESSION,user);
			return "redirect:zhu";
		}else{
			request.setAttribute("error","用户名或密码错误！！！");
			return "denglu";
		}
	}
	@RequestMapping(value="/zhu",method=RequestMethod.GET)
	public String zhu(Model model,
			@RequestParam(value="username",required=false)String username,
			@RequestParam(value="pageIndex",required=false)String pageIndex){
		List<User> list =null;
		//设置页面容量-每页显示的条数默认是5
    	int pageSize = Constants.pageSize;
    	//当前页码
    	int currentPageNo = 1;
    	if(username == null){
    		username = "";
		}
    	if(pageIndex != null && pageIndex != ""){
    		try{
    			currentPageNo = Integer.valueOf(pageIndex);
    		}catch(NumberFormatException e){
    			return "redirect:/syserror.html";
    		}
    	}	
    	//总数量（表）	
    	int totalCount = 0;
    	totalCount = userService.getUserCount(username);
    	//总页数
    	PageSupport pages=new PageSupport();
    	pages.setCurrentPageNo(currentPageNo);
    	pages.setPageSize(pageSize);
    	pages.setTotalCount(totalCount);
    	int totalPageCount = pages.getTotalPageCount();
    	//控制首页和尾页
    	if(currentPageNo < 1){
    		currentPageNo = 1;
    	}else if(currentPageNo > totalPageCount){
    		currentPageNo = totalPageCount;
    	}
    	list = userService.getUser(username, currentPageNo, pageSize);
    	model.addAttribute("userlist",list);
    	model.addAttribute("username",username);
    	model.addAttribute("totalPageCount", totalPageCount);//总页
		model.addAttribute("totalCount", totalCount);//总条数
		model.addAttribute("currentPageNo", currentPageNo);//当前页
		return "index";
	}
	
	
	@RequestMapping(value="/tuichu",method=RequestMethod.GET)
	public String tuichu(HttpSession sessiion){
		//清除session
		sessiion.removeAttribute(Constants.USER_SESSION);
		return "denglu";
	}
	
	@RequestMapping(value="/update",method=RequestMethod.GET)
	public String xiugai(int id,Model model){
		User user = userService.getUserId(id);
		model.addAttribute("id",user.getId());
		model.addAttribute("name",user.getName());
		model.addAttribute("mima",user.getMima());
		return "xiugai";
	}
	@RequestMapping(value="/xg",method=RequestMethod.GET)
	public String xiugai1(User user){
		userService.updateUser(user);
		return "redirect:zhu";
	}
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String tianjia(User user){
		userService.addUser(user);
		return "redirect:zhu";
	}
	@RequestMapping(value="/delete",method=RequestMethod.GET)
	public String shanchu(int id){
		userService.deleteUser(id);
		return "redirect:zhu";
	}
}
